package leetcode.primary;

public class T309 {
    public int maxProfit(int[] prices) {
        int length = prices.length;
        if (length==1){
            return 0;
        }
        int[] profits = new int[length];
        profits[length-1] = 0;
        int repeat = 1;
        for (int i = prices.length-2; i >=0; i--) {
            int tempProfit = prices[i+1] - prices[i];
            if (tempProfit>0){
                if (repeat==2){
                    if (tempProfit>profits[i+1]){
                        profits[i] = tempProfit;
                        repeat=1;
                    }else {
                        profits[i] = profits[i+1];
                        repeat++;
                    }
                }else {
                    profits[i] = profits[i+1]+tempProfit;
                    repeat = 1;
                }
            }else {
                profits[i] = profits[i+1];
                repeat++;
            }
            System.out.print(repeat+" ");
        }
        System.out.println();
        for (int profit : profits) {
            System.out.print(profit+" ");
        }
        System.out.println();
        return profits[0];
    }

    public static void main(String[] args) {
        T309 t309 = new T309();
        int maxProfit = t309.maxProfit(new int[]{6,1,3,2,4,7});
        System.out.println(maxProfit);
    }
}
